<?php
/**
 * Description of feed_vnexpress
 *
 * @author phuchv
 */

require APPPATH . 'libraries/simple_html_dom.php';

class feed_baohaiquan {

    public function doFeed() {
        $site = "http://www.baohaiquan.vn";
        
        // get DOM from URL or file
        $html = file_get_html('http://www.baohaiquan.vn/pages/default.aspx');
        $html_TinDocNhieu = $html->find('div[id=tindocphanhoi1nd]', 0);
        // Find all links
        foreach ($html_TinDocNhieu->find('a') as $element) {

            try {

                $html_detail_TinDocNhieu = file_get_html($site . $element->href);
                echo $element->href;
                echo "<br/>";
               
                $printNews = $html_detail_TinDocNhieu->find('div[class=article-header]', 0);
                foreach ($printNews->find('h1') as $title) {
                    echo trim($title->innertext);
                }

                echo "</br>";

                foreach ($printNews->find('div[class=article-des article-des02] p') as $shortDesc) {
                    echo $shortDesc->innertext;
                }

                echo "</br>";

                // Find all li elements
                foreach ($printNews->find('img') as $img) {
                    $src = $site . $img->src;

                    $path = parse_url($src, PHP_URL_PATH);
                    $filename = basename($path);
                    $filename = str_replace(" ", "", $filename);
                    $dest = $_SERVER['DOCUMENT_ROOT'] . "/style/upimg_man/image/News/" . $filename;

                    $image = "<img src='" . $site . $img->src . "' />";
                    echo $image;

                    //copy($src, $dest);
                    @$rawImage = file_get_contents($src);
                    if ($rawImage) {
                        file_put_contents($dest, $rawImage);
                    }
                }

                echo "</br>";

                foreach ($printNews->find('div[class=article-content article-content02]') as $body) {
                    echo $body->innertext;
                }
            } catch (Exception $ex) {
                //echo $ex->getTraceAsString();
            }
        }
    }

}

?>
